from django.http import HttpResponse
from django.shortcuts import render, HttpResponse, redirect
from pet.models import Department, Userinfo, Cat, Mouse, Dog


# 把从数据取出来的数据变成一个列表，因为从数据取出的是查询列表，不好操作数据
def QuerySetToDictlist(qd):
    mylist = []
    for v in qd:
        mylist.append(v)
    return mylist


# Create your views here.
def index(request):
    return render(request, 'pet/index.html',
                  {"cat_list": QuerySetToDictlist(Cat.objects.using('pet').values())[0:8],
                   "dog_list": QuerySetToDictlist(Dog.objects.using('pet').values())[0:8],
                   "mouse_list": QuerySetToDictlist(Mouse.objects.using('pet').all().values())[0:8]})


def cat(request):
    # cat_list = Cat.objects.all()
    return render(request, 'pet/cat.html', {"cat_list": QuerySetToDictlist(Cat.objects.using('pet').values())})


def cat_add(request):
    # get post get用于请求页面 post提交数据
    if request.method == "POST":
        # 获取提交的数据
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        if not name:
            return render(request, 'pet/cat_add.html', {'error': "猫猫名字不能为空"})
        if Cat.objects.using('pet').filter(name=name):
            return render(request, 'pet/cat_add.html', {'error': "猫猫已经存在"})
        Cat.objects.using('pet').create(name=name, another_name=another_name, lifetime=lifetime, area_of_origin=area_of_origin,
                           price=price, habit=habit, picture=picture)
        return redirect('/pet/cat')  # 增加完成以后，跳转到猫猫页面
    return render(request, 'pet/cat_add.html')  # 默认返回增加页面。


def cat_del(request):
    cid = request.GET.get('id')  # 获取URL中id字段的值
    Cat.objects.using('pet').filter(id=cid).delete()  # 删除数据库中的数据
    return redirect('/pet/cat')  # 重定向页面。


def cat_edit(request):
    cid = request.GET.get('id')
    obj1 = Cat.objects.using('pet').get(id=cid)
    if request.method == "GET":
        return render(request, 'pet/cat_edit.html', {'cat_obj': obj1})
    else:
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        obj2 = Cat.objects.using('pet').filter(name=name)
        if obj2:
            return render(request, 'pet/cat_edit.html', {'msg': "该猫咪已存在"})
        else:
            obj1.name = name
            obj1.another_name = another_name
            obj1.lifetime = lifetime
            obj1.area_of_origin = area_of_origin
            obj1.price = price
            obj1.habit = habit
            obj1.picture = picture
            obj1.save()  # 将修改保存到数据库
            return redirect('/pet/cat')  # 回到到cat页面


def dog(request):
    return render(request, 'pet/dog.html', {"dog_list": QuerySetToDictlist(Dog.objects.using('pet').all().values())})


def dog_add(request):
    if request.method == "POST":
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        if not name:
            return render(request, 'pet/dog_add.html', {'error': "狗狗名字不能为空"})
        if Dog.objects.using('pet').filter(name=name):
            return render(request, 'pet/dog_add.html', {'error': "狗狗已经存在"})
        Dog.objects.using('pet').create(name=name, another_name=another_name, lifetime=lifetime, area_of_origin=area_of_origin,
                           price=price, habit=habit, picture=picture)
        return redirect('/pet/dog')  # 增加完成以后，跳转到狗狗页面
    return render(request, 'pet/dog_add.html')  # 默认返回增加页面。


def dog_del(request):
    did = request.GET.get('id')  # 获取URL中id字段的值
    Dog.objects.using('pet').filter(id=did).delete()  # 删除数据库中的数据
    return redirect('/pet/dog')  # 重定向页面。


def dog_edit(request):
    did = request.GET.get('id')
    obj1 = Dog.objects.using('pet').get(id=did)
    if request.method == "GET":
        return render(request, 'pet/dog_edit.html', {'dog_obj': obj1})
    else:
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        obj2 = Dog.objects.using('pet').filter(name=name)
        if obj2:
            return render(request, 'pet/dog_edit.html', {'msg': "该狗狗已存在"})
        else:
            obj1.name = name  # 在内存中修改
            obj1.another_name = another_name
            obj1.lifetime = lifetime
            obj1.area_of_origin = area_of_origin
            obj1.price = price
            obj1.habit = habit
            obj1.picture = picture
            obj1.save()  # 将修改保存到数据库
            return redirect('/pet/dog')  # 回到狗狗页面


def pet(request):
    return render(request, 'pet/pet.html', {"mouse_list": QuerySetToDictlist(Mouse.objects.using('pet').all().values())})


def pet_add(request):
    if request.method == "POST":
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        if not name:
            return render(request, 'pet/pet_add.html', {'error': "鼠鼠名字不能为空"})
        if Mouse.objects.using('pet').filter(name=name):
            return render(request, 'pet/pet_add.html', {'error': "鼠鼠已经存在"})
        Mouse.objects.using('pet').create(name=name, another_name=another_name, lifetime=lifetime, area_of_origin=area_of_origin,
                             price=price, habit=habit, picture=picture)
        return redirect('/pet/pet')  # 增加完成以后，跳转到鼠鼠页面
    return render(request, 'pet/pet_add.html')  # 默认返回增加页面。


def pet_del(request):
    pid = request.GET.get('id')  # 获取id
    Mouse.objects.using('pet').filter(id=pid).delete()  # 删除数据库中的数据
    return redirect('/pet/pet')  # 回到鼠鼠页面。


def pet_edit(request):
    pid = request.GET.get('id')
    obj1 = Mouse.objects.using('pet').get(id=pid)
    if request.method == "GET":
        return render(request, 'pet/pet_edit.html', {'pet_obj': obj1})
    else:
        name = request.POST.get('name')
        another_name = request.POST.get('another_name')
        lifetime = request.POST.get('lifetime')
        area_of_origin = request.POST.get('area_of_origin')
        price = request.POST.get('price')
        habit = request.POST.get('habit')
        picture = request.POST.get('picture')
        obj2 = Mouse.objects.using('pet').filter(name=name)
        if obj2:
            return render(request, 'pet/pet_edit.html', {'msg': "鼠鼠已存在"})
        else:
            obj1.name = name  # 在内存中修改
            obj1.another_name = another_name
            obj1.lifetime = lifetime
            obj1.area_of_origin = area_of_origin
            obj1.price = price
            obj1.habit = habit
            obj1.picture = picture
            obj1.save()  # 将修改保存到数据库
            return redirect('/pet/pet')  # 回到鼠鼠页面


def orm(request):
    # 1.新建
    # Department.objects.create(title="销售部")
    # Department.objects.create(title="IT部")
    # Department.objects.create(title="运营部")
    # UserInfo.objects.create(name="xyy", password="123", age=19)
    # UserInfo.objects.create(name="xxx", password="123", age=20)
    # UserInfo.objects.create(name="yyy", password="123", age=21)
    # 2.删除
    # UserInfo.objects.filter(id=3).delete()
    # Department.objects.all().delete()
    # 3.获取数据(获取符合条件的数据)
    # data_list = UserInfo.objects.all()
    # for obj in data_list:
    #     print(obj.id, obj.name, obj.password, obj.age)
    # 4.更新数据
    # UserInfo.objects.all().update(password=999)
    # UserInfo.objects.filter(id="2").update(age=50)
    return HttpResponse("成功")


def info_list(request):
    # 1.获取数据库中的信息
    data_list = Userinfo.objects.using('pet').all()
    # 2.渲染，返回给用户
    return render(request, "info_list.html", {"data_list": data_list})


def info_add(request):
    if request.method == "GET":
        return render(request, 'info_add.html')
    # 获取用户提交的数据
    name = request.POST.get("name")
    lifetime = request.POST.get("lifetime")
    price = request.POST.get("price")
    # 添加到数据库
    Userinfo.objects.using('pet').create(name=name, lifetime=lifetime, price=price)
    # return HttpResponse("添加成功")
    # 添加成功后使其自动跳转到列表页面
    return redirect("/info/list/")


def info_delete(request):
    nid = request.GET.get('nid')
    Userinfo.objects.using('pet').filter(id=nid).delete()
    return redirect("/info/list/")
